/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package metricmaster.gui.projecttab;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import metricmaster.app.ProjectRepository;
import metricmaster.gui.AppData;
import org.joda.time.DateTime;
import org.joda.time.Weeks;

/**
 *
 * @author markblanda
 */
public class ProjectPanel extends JPanel implements ActionListener {
    private JButton loadButton = new JButton("Load");
    private JButton addButton = new JButton("Save");
    private JTextField nameField = new JTextField(15);
    private JTextField startField = new JTextField(10);
    private JTextField finishField = new JTextField(10);
    private JTextField weeksField = new JTextField(5);
    private GridBagConstraints c = new GridBagConstraints();
    private Date startDate;
    private Date finishDate;
    
    private ProjectRepository projectRepo;
    SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
    
    private AppData appData = AppData.getInstance();
    
    public ProjectPanel(ProjectRepository projectRepository) {
    	this.projectRepo = projectRepository;
        this.setLayout(new GridBagLayout());
        setupComponents();
        populateComponents();
        this.setBorder(new TitledBorder("Projects"));
    }
    
    private void populateComponents() {
    	nameField.setText(appData.getProject().getName());
    	startField.setText(format.format(appData.getProject().getStart()));
    	finishField.setText(format.format(appData.getProject().getFinish()));
    }
    
    private void setupComponents() {
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        this.add(new JLabel("Name:"), c);
        
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 0;
        this.add(nameField, c);
        
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        this.add(new JLabel("Start:"), c);
        
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 1;
        this.add(startField, c);
        
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 2;
        this.add(new JLabel("Finish:"), c);
        
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 2;
        this.add(finishField, c);
        
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 3;
        this.add(addButton, c);
        
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 3;
        this.add(loadButton, c);
        
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 4;
        this.add(new JLabel("Weeks:"), c);       
        
        //c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 4;
        this.add(weeksField, c);
        
        addButton.addActionListener(this);
        loadButton.addActionListener(this);
    }

	@Override
	public void actionPerformed(ActionEvent e) {
            if (e.getSource() == addButton && checkDate()) {
		//update project information
		appData.getProject().setName(nameField.getText());
		appData.getProject().setStart(startDate);
		appData.getProject().setFinish(finishDate);
                DateTime finish = new DateTime(finishDate);
                DateTime start = new DateTime(startDate);
                appData.getProject().setWeeks(Weeks.weeksBetween(start, finish).getWeeks());
                weeksField.setText(appData.getProject().getWeeks().toString());
		
		//save to file
		projectRepo.saveProject(appData.getProject());
                
            } else if (e.getSource() == loadButton) {
                appData.setProject(projectRepo.loadProject(nameField.getText()));
            }
	}
        
        private boolean checkDate() {
            boolean goodDate = true;
            
        try {
            System.out.println("Converting " + startField.getText());
            startDate = format.parse(startField.getText());
            System.out.println("Formatted: " + format.format(startDate));
            finishDate = format.parse(finishField.getText());
        } catch (ParseException ex) {
            Logger.getLogger(ProjectPanel.class.getName()).log(Level.SEVERE, null, ex);
            System.err.println(ex.getMessage());
            goodDate = false;
        }
            
            return goodDate;
        }

}
